<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Config\Mapper;

use App\Config\Model\ConfigProduct;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 饰品列表Mapper类
 */
class ConfigProductMapper extends AbstractMapper
{
    /**
     * @var ConfigProduct
     */
    public $model;

    public function assignModel()
    {
        $this->model = ConfigProduct::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 类型(1.csgo,2.商品)
        if (isset($params['source_type']) && filled($params['source_type'])) {
            if($params['source_type']==$this->model::SOURCE_TYPE_CSGO){
                $query->where('price', '>', 0)
                      ->where('stock', '>', 5)
                      ->whereIn('category_name', ['匕首', '步枪', '机枪', '狙击步枪', '手枪', '手套', '微型冲锋枪', '霰弹枪']); //固定8种类型
            }
            $query->where('source_type', '=', $params['source_type']);
        }

        // 销售价格
        if (isset($params['price']) && filled($params['price'])) {
            $query->where('price', '=', $params['price']);
        }

        // 采购价格
        if (isset($params['origin_price']) && filled($params['origin_price'])) {
            $query->where('origin_price', '=', $params['origin_price']);
        }
        if (isset($params['amount_price_rangeMin']) && filled($params['amount_price_rangeMin'])) {
            $query->where('origin_price', '>=', $params['amount_price_rangeMin'] * \App\Constants\AdminEnum::PRICE_MULTIPLE);
        }
        if (isset($params['amount_price_rangeMax']) && filled($params['amount_price_rangeMax'])) {
            $query->where('origin_price', '<=', $params['amount_price_rangeMax'] * \App\Constants\AdminEnum::PRICE_MULTIPLE);
        }

        if (isset($params['price_rangeMin']) && filled($params['price_rangeMin'])) {
            $query->where('price', '>=', $params['price_rangeMin'] * \App\Constants\AdminEnum::PRICE_MULTIPLE);
        }
        if (isset($params['price_rangeMax']) && filled($params['price_rangeMax'])) {
            $query->where('price', '<=', $params['price_rangeMax'] * \App\Constants\AdminEnum::PRICE_MULTIPLE);
        }
        // 人民币价格
        if (isset($params['rmb_price']) && filled($params['rmb_price'])) {
            $query->where('rmb_price', '=', $params['rmb_price']);
        }

        // 产品名称
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 商品原名称
        if (isset($params['origin_name']) && filled($params['origin_name'])) {
            $query->where('origin_name', 'like', '%'.$params['origin_name'].'%');
        }

        // 英文名称
        if (isset($params['english_name']) && filled($params['english_name'])) {
            $query->where('english_name', 'like', '%'.$params['english_name'].'%');
        }

        // 描述与介绍
        if (isset($params['introduce']) && filled($params['introduce'])) {
            $query->where('introduce', 'like', '%'.$params['introduce'].'%');
        }

        // 商品图片
        if (isset($params['cover']) && filled($params['cover'])) {
            $query->where('cover', 'like', '%'.$params['cover'].'%');
        }

        // 库存
        if (isset($params['stock']) && filled($params['stock'])) {
            $query->where('stock', '=', $params['stock']);
        }

        // 外部标签
        if (isset($params['tag_exterior']) && filled($params['tag_exterior'])) {
            $query->where('tag_exterior', 'like', '%'.$params['tag_exterior'].'%');
        }

        // 内部标签
        if (isset($params['tag_internal']) && filled($params['tag_internal'])) {
            $query->where('tag_internal', 'like', '%'.$params['tag_internal'].'%');
        }

        // 内部概率
        if (isset($params['rate_inside']) && filled($params['rate_inside'])) {
            $query->where('rate_inside', '=', $params['rate_inside']);
        }

        // 外部概率
        if (isset($params['rate_out']) && filled($params['rate_out'])) {
            $query->where('rate_out', '=', $params['rate_out']);
        }

        // 分类ID
        if (isset($params['category_id']) && filled($params['category_id'])) {
            $query->where('category_id', '=', $params['category_id']);
        }

        // 分类名称
        if (isset($params['category_name']) && filled($params['category_name'])) {
            $query->where('category_name', 'like', '%'.$params['category_name'].'%');
        }

        // 唯一值（对应三方键值）
        if (isset($params['item_id']) && filled($params['item_id'])) {
            $query->where('item_id', 'like', '%'.$params['item_id'].'%');
        }

        // 扩展字段
        if (isset($params['extend_field']) && filled($params['extend_field'])) {
            $query->where('extend_field', 'like', '%'.$params['extend_field'].'%');
        }

        // 上下架（1上架,2下架）
        if (isset($params['up_status']) && filled($params['up_status'])) {
            $query->where('up_status', '=', $params['up_status']);
        }

        // 物品等级
        if (isset($params['grade']) && filled($params['grade'])) {
            $query->where('grade', 'like', '%'.$params['grade'].'%');
        }

        // 状态(1正常,2关闭)
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 创建时间
        if (isset($params['create_time']) && filled($params['create_time'])) {
            $query->where('create_time', '=', $params['create_time']);
        }

        // 更新时间
        if (isset($params['update_time']) && filled($params['update_time'])) {
            $query->where('update_time', '=', $params['update_time']);
        }

        // 
        if (isset($params['delete_time']) && filled($params['delete_time'])) {
            $query->where('delete_time', '=', $params['delete_time']);
        }

        return $query;
    }

    public function getByIds(array $ids, ?array $select = null): array
    {
        if (!$select) {
            $select = ['id', 'name', 'price', 'origin_price', 'cover', 'create_time'];
        }
        return $this->model::query()->whereIn('id', $ids)->select($select)->get()->toArray();
    }

    /**
     * 获取列表数据（带分页）
     * @param array|null $params
     * @param bool $isScope
     * @param string $pageName
     * @return array
     */
    public function getZbtPageList(?array $params, bool $isScope = true, string $pageName = 'page'): array
    {
        $paginate = $this->listQuerySetting($params, $isScope)->paginate(
            (int)($params['pageSize'] ?? $this->model::PAGE_SIZE), ['*'], $pageName, (int)($params[$pageName] ?? 1)
        );

        $markName = [];
        $markIdsName=[];
        foreach ($paginate->items() as $item) {
            $markName[] = $item->english_name;
            $markIdsName[$item->item_id]=$item->english_name;
        }


        if(!empty(count($paginate->items()))){
            $postRes=\App\Lib\Library\ZbtQuery::queryPriceBathName($markName);

            foreach ($paginate->items() as &$item) {
                $item->zbt_remote_price=empty($postRes[$item->item_id])?'查无数据':$postRes[$item->item_id]['price'];
                $item->autoDeliverQuantity=empty($postRes[$item->item_id])?0:$postRes[$item->item_id]['autoDeliverQuantity'];
                $item->manualQuantity=empty($postRes[$item->item_id])?0:$postRes[$item->item_id]['manualQuantity'];
            }
        }

        $res = $this->setPaginate($paginate, $params);
        return $res;
    }
}